//Nathan Popham
//4003-573-01: Procedural Shading
//Lab 5
//
//myspotlight.sl
// Directed light shader.
//
//intensity -- color of the light
//field -- maximum spotlight angle
//beam -- spotlight angle of brightest area
//from -- location of spotlight
//to -- point spotlight is aiming at
//distance -- when greater than 0.0, distance attenuation enabled
//focus -- when 1.0, field angle effect removed; when 0.0, field angle
//         effect normal; when between, field angle effect interpolated

light
myspotlight (
             color intensity = 1.0; 
             float field = 20.0;
             float beam = 10.0;
	         point from = point "shader" (0,0,0);
             point to = point "shader" (0, 0, 0);
             float distance = 0.0;
             float focus = 0.5;
            )
{

    float sqr(float xx) { return xx * xx; }
    
    illuminate (from) {
        normal lightDir = normalize(to - from);

        float lightDotSurface = lightDir . normalize(L);   
        float angle = abs(acos(lightDotSurface)) * 180 / PI;
        
        float dAttenuation = distance > 0.0 ? .3*sqrt(L . L) : 1.0;
        float effectiveField = (field - beam) * (1.0 - focus) + beam;
        
        if(angle <= beam)
            Cl = intensity * (1.0 - .5*smoothstep(0.0, beam, angle)) / dAttenuation;
        else if(angle <= effectiveField)
            Cl = intensity * (.5 - .4*smoothstep(beam, effectiveField, angle)) / dAttenuation;
        else
            Cl = 0;
   }
}
